package com.atguigu.gulimall.ware.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.zaxxer.hikari.HikariDataSource;
import io.seata.rm.datasource.DataSourceProxy;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;

/**
 * @ClassName HuangXiangXiang
 * @Date 2020/7/26 13:02
 * @Version V1.0
 **/


@MapperScan("com.atguigu.gulimall.ware.dao")
@EnableTransactionManagement
@Configuration
public class WareMybatisConfig {

    //引入分页插件
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 设置请求的页面大于最大页后操作， true调回到首页，false 继续请求  默认false
//        paginationInterceptor.setOverflow(true);
//        // 设置最大单页限制数量，默认 500 条，-1 不受限制
//        paginationInterceptor.setLimit(1000);
        return paginationInterceptor;
    }



/*seata 分布式事务*/
    @Autowired
    DataSourceProperties dataSourceProperties;

    @Bean
    public DataSource dataSource(DataSourceProperties dataSourceProperties) {
        //用这个代理把我们原来的数据源包装、包装以后我们的自定义的数据源就放进去了
        //这个服务这样定义数据源、其他服务都一样
        HikariDataSource dataSource = dataSourceProperties.initializeDataSourceBuilder ().type (HikariDataSource.class).build ();
        if (StringUtils.hasLength (dataSourceProperties.getName ())) {
            dataSource.setPoolName (dataSourceProperties.getName ());
        }
        return new DataSourceProxy (dataSource);
    }
}
